package com.example.chamberlainserver.controller;

import com.example.chamberlainserver.Entry.UserActionLog;
import com.example.chamberlainserver.Service.UserLogService;
import com.example.chamberlainserver.Vo.Response.ApiResponse;
import com.example.chamberlainserver.dto.PageResult;
import com.example.chamberlainserver.dto.UserActionLogQueryDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 用户操作日志控制器
 */
@RestController
@RequestMapping("/logs/action")
public class UserActionLogController {
    
    @Autowired
    private UserLogService userLogService;
    
    /**
     * 分页查询操作日志
     * @param queryDTO 查询条件
     * @return 分页结果
     */
    @PostMapping("/page")
    public ResponseEntity<ApiResponse<PageResult<UserActionLog>>> pageActionLogs(@RequestBody UserActionLogQueryDTO queryDTO) {
        PageResult<UserActionLog> pageResult = userLogService.pageActionLogs(queryDTO);
        return ResponseEntity.ok(ApiResponse.success("查询操作日志成功", pageResult));
    }
    
    /**
     * 获取操作类型分布
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return 操作类型分布数据
     */
    @GetMapping("/operation-distribution")
    public ResponseEntity<ApiResponse<List<java.util.Map<String, Object>>>> getOperationDistribution(
            @RequestParam(required = false) LocalDateTime startTime,
            @RequestParam(required = false) LocalDateTime endTime) {
        // 如果未指定时间范围，默认查询最近30天
        if (startTime == null) {
            startTime = LocalDateTime.now().minusDays(30);
        }
        if (endTime == null) {
            endTime = LocalDateTime.now();
        }
        
        List<java.util.Map<String, Object>> distribution = userLogService.getOperationDistribution(startTime, endTime);
        return ResponseEntity.ok(ApiResponse.success("获取操作类型分布成功", distribution));
    }
    
    /**
     * 获取模块访问分布
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return 模块访问分布数据
     */
    @GetMapping("/module-distribution")
    public ResponseEntity<ApiResponse<List<java.util.Map<String, Object>>>> getModuleDistribution(
            @RequestParam(required = false) LocalDateTime startTime,
            @RequestParam(required = false) LocalDateTime endTime) {
        // 如果未指定时间范围，默认查询最近30天
        if (startTime == null) {
            startTime = LocalDateTime.now().minusDays(30);
        }
        if (endTime == null) {
            endTime = LocalDateTime.now();
        }
        
        List<java.util.Map<String, Object>> distribution = userLogService.getModuleDistribution(startTime, endTime);
        return ResponseEntity.ok(ApiResponse.success("获取模块访问分布成功", distribution));
    }
}